/**
 * @author: yd
 * @recentUpdate: 2020-05-14
 * @Time: 15:07
 * @CopyRight sr  All rights reserved
 * @Notice: 该文件为请求的统一返回格式，如在此文件做任何改动请详细写明目的(注释),否则出现问题后果自负
 */
module.exports = (options = {}) => {
  return async (ctx, next) => {
    ctx.success = (data, message = 'success', code = 0) => {
      ctx.body = {
        headers: { code: code, message: message },
        data: data
      }
    };
    ctx.fail = (conf = 'fail', code = -1) => {
      if(typeof conf === 'object') {
        ctx.status = conf.status || 200;
        ctx.body = {
          headers: { code: conf.code || -1, message: conf.message || 'fail' },
          error: conf
        };
      }else {
        ctx.body = {
          headers: { code: code, message: conf }
        };
      }

    };
    await next();
  }
};
